package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Coupon;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
* @author circle48
* @description 针对表【coupon(优惠券表)】的数据库操作Mapper
* @createDate 2024-05-28 18:09:06
* @Entity com.example.entity.Coupon
*/
@Mapper
public interface CouponMapper extends BaseMapper<Coupon> {
    @Update("UPDATE coupon set receive_count  =  receive_count+1 WHERE id = #{id} AND receive_count < #{publishCount}")
    int incrReceiveCount(@Param("id") Long couponId, @Param("publishCount") Integer publishCount);


    @Select("SELECT * FROM coupon WHERE store_id = #{storeId} "
            + "AND start_time <= NOW() AND end_time >= NOW()")
    List<Coupon> getCouponsByStoreId(Long storeId);

    @Select("SELECT * FROM coupon WHERE store_id = #{storeId} AND start_time <= NOW() AND end_time >= NOW()")
    List<Coupon> selectUserCouponsByStore(Long storeId);

    @Update("update coupon set use_count=use_count+1 where id=#{couponId}")
    void updateUseCount(Long couponId);
}




